package com.zyg.user.service.Impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.zyg.user.exception.MyException;
import com.zyg.user.handler.MyBlockHandler;
import com.zyg.user.handler.MyFallBackHandler;
import com.zyg.user.service.SentinelService;
import org.springframework.stereotype.Service;


@Service
public class SentinelServiceImpl implements SentinelService {
    @Override
    @SentinelResource("message")
    public String message() {
        System.out.println("执行链路调用");
        return "message";
    }

    //@SentinelResource 此注解配置可以定制对两组异常的处理规则
//    @Override
//    @SentinelResource(value = "myMsg",blockHandler = "myBlockHandler",fallback = "myFallBackHandler")
//    public String getMessage() {
//        int i = 10/0;
//        return "getMessage";
//    }

    @Override
    @SentinelResource(value="myMsg",blockHandlerClass = MyBlockHandler.class,blockHandler = "myBlockHandler2",
            fallback = "myFallBackHandler2",fallbackClass = MyFallBackHandler.class,
            exceptionsToIgnore = MyException.class)
    public String getMessage() {
        int i = 10 / 0;
        return "getMessage";
    }
    //处理BlockException


    public String myBlockHandler(BlockException ex){
        String msg = ex.getMessage();
        if (StringUtil.isBlank(msg)){
            msg = "出现了BlockException异常!";
        }
        return "myBlockHander:"+msg;
    }

    public String myFallBackHandler(Throwable t){
        String msg = t.getMessage();
        if (StringUtil.isBlank(msg)){
            msg = "出现了Throwable异常!";
        }
        return "myFallBackHandler:"+msg;
    }
}
